<template>
    <div class="goods-container">
        <div class="good-item" v-for="item in list" @click="getInfo(item.id)">
            <img src="https://img.alicdn.com/imgextra/i1/901409638/TB2NuGLy3mTBuNjy1XbXXaMrVXa_!!901409638-0-item_pic.jpg_250x250Q90.jpg_.webp">
            <h1>{{ item.title }}</h1>
            <div class="info">
                <div class="substr">
                    <span class="new">￥{{ item.sell_price }}</span>
                    <span class="old">￥{{ item.market_price }}</span>
                </div>
                <div class="status">
                    <span>热卖中</span>
                    <span>剩{{ item.stock_quantity }}件</span>
                </div>
            </div>
        </div>
        <mt-button type="danger" size="large" @click="getMore">加载更多</mt-button>
    </div>
</template>

<script>
import Toast from 'mint-ui'

export default {
    data () {
        // data 是往自己组件内部，挂载一些私有数据的
        return {
            list:[],
            pageindex:1
        }
    },
    created () {
        this.getGoodsList()
        
    },
    methods: {
        getGoodsList () {
            this.$http.get(`api/getgoods?${this.pageindex}`).then(res =>{
               if(res.body.status === 0) {
                   this.list = this.list.concat(res.body.message)
               } else {
                   Toast('商品列表加载失败！')
               }
            })
        },
        getMore() {
            this.pageindex ++
            this.getGoodsList()
        },
        getInfo (id) {
            // 利用编程方式导航路由链接
            // this.$route 是路由{参数对象}，所有路由的参数，params，query都属于他，路径中的参数
            // this.$router 是一个路由{导航对象},用它 可以方便的使用js代码，实现路由的前进，后退，跳转到新的地址
            // 第一种字符串
            // this.$router.push('/home/goodsinfo'+id)
            // 第二种传递对象
        //    this.$router.push({path:'/home/goodsinfo/'+id})
            // 第三种传递命名路由
            this.$router.push({name:'goodsinfo',params:{id}})
        }
    }
};
</script>

<style lang="scss" scoped>
.goods-container {
  background-color: whitesmoke;

    display: flex;
    flex-wrap: wrap;
    padding: 7px;
    justify-content: space-between;
    // 利用flex弹性盒子实现布局

  .good-item {
     width: 49%;
     border: 1px solid #ccc;
     border-radius: 5px;
     box-shadow: 0 0 8px #ccc;
     margin: 4px 0;
     padding: 4px;
     display: flex;
     flex-direction: column;
     justify-content: space-between;
    //  改变主轴。利用flex盒子布局
     h1{
         font-size: 16px;
         font-weight: bold;
     }
     img{
         width: 100%;
         min-height: 170px;
     }
     .info{
         background-color: #ccc;
         border-radius: 5px;
         .substr{
             .new{
                 color:red;
             }
             .old{
                 font-size: 13px;
                 text-decoration: line-through;
             }
         }
         .status{
             display: flex;
             justify-content: space-between;
             font-size: 14px;
         }
     }     

  }
}
</style>
